from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
from tkinter import filedialog
import shutil


class app(Tk):
    def __init__(self):
        super().__init__()
        self.body_width1 = int(self.winfo_screenwidth() / 4.5)
        self.body_height1 = int(self.winfo_screenheight() / 10)
        self.geometry(f"880x700+{self.body_width1}+{self.body_height1}")
        self.resizable(0, 0)
        self.title("文件读取器")
        self.a = StringVar()
        self.a.set("开始文件读取，筛选操作前请先选择文件————————>")
        self.movevar = StringVar()
        self.movevar.set("如需复制文件，请先选择文件复制的路径————————>")
        self.sta = StringVar()
        self.end = StringVar()
        self.filea = []
        self.test111 = 111
        ##row0
        self.bo = Button(self, text="文件选择", command=self.bofilexuanzhe).grid(column=5, row=0, sticky="ewns")
        self.bo1 = Button(self, text="文件读取", command=self.clickfile).grid(column=6, row=0, sticky="ewns")
        self.txt = Label(self, textvariable=self.a, width=100, height=3).grid(column=0, columnspan=5, row=0,
                                                                              sticky="nsew")
        ##row1
        self.filepathinput = Label(self, width=20, height=3, textvariable=self.movevar)
        self.filepathinput.grid(column=0, columnspan=5, row=1, sticky="ewns")
        self.filepathbo = Button(self, text="选择路径", width=5, height=3, command=self.bofilemove).grid(row=1, column=5,
                                                                                                     sticky="nswe")
        self.filepath1 = Button(self, text="开始复制", width=5, height=3, command=self.move).grid(row=1, column=6,
                                                                                              sticky="nsew")
        ##row2
        self.filesizekaishitishi = Label(self, text="筛选开始大小(单位byte)").grid(column=0, row=2, sticky="ewns")
        self.filesizeinputstra = Text(self, width=20, height=3)
        self.filesizeinputstra.grid(column=1, row=2, sticky="ewns")
        self.filesizejieshutishi = Label(self, text="筛选结束大小(单位byte)").grid(column=2, row=2, sticky="ewns")
        self.filesizeinputendd = Text(self, width=20, height=3)
        self.filesizeinputendd.grid(column=3, row=2, sticky="ewns")
        self.bo2 = Button(self, text="文件大小筛选", command=self.bofilesize).grid(column=5, columnspan=2, row=2,
                                                                             sticky="ewns")
        ##row3
        self.filetypetip = Label(self, text="筛选的文件类型(例：.txt)").grid(column=0, row=3, sticky="nswe")
        self.filetypeinput = Text(self, width=20, height=3)
        self.filetypeinput.grid(column=1, row=3, sticky="ewns")
        self.filetype = Button(self, text="文件类型筛选", width=10, height=3, command=self.bofiletype).grid(row=3, column=2,
                                                                                                      sticky="nswe")
        self.filenameinput = Text(self, width=20, height=3)
        self.filenameinput.grid(column=3, row=3, sticky="ewns")
        self.filename11 = Button(self, text="文件名称筛选", width=10, height=3, command=self.find_files).grid(row=3, column=5,
                                                                                                        sticky="nswe")
        ##row4
        self.txtt = Text(self, width=110, height=30)
        self.txtt.grid(row=4, column=0, columnspan=7)
        #self.ezj()
    def move(self):
        if not self.a.get() or self.a.get()=="开始文件读取，筛选操作前请先选择文件————————>":
            messagebox.showinfo(" ", "请先选择文件")
        elif not self.movevar.get() or self.movevar.get()=="如需复制文件，请先选择文件复制的路径————————>":
            messagebox.showinfo(" ", "请输入目标地址")
        elif not self.filea:
            messagebox.showinfo(" ", "没有可移动的文件")
        else:
            self.txtt.delete(1.0, END)
            self.qr=messagebox.askyesno("",f"将要复制文件到{self.movevar.get()}")
            if self.qr==True:
                for i in self.filea:
                    self.txtt.insert(INSERT, f"{i}------复制完成\n")
                    shutil.copy2(i, self.movevar.get())


    def bofiletype(self):
        if not self.a.get() or self.a.get()=="开始文件读取，筛选操作前请先选择文件————————>":
            messagebox.showinfo(" ", "请先选择文件")
        elif self.filetypeinput.get(1.0,END) == "\n":
            messagebox.showinfo(" ", "请输入筛选的文件类型")
        else:
            self.filea = []
            self.txtt.delete(1.0, END)
            files = os.walk(self.c)
            for root, dir, filelist in files:
                for file in filelist:
                   if os.path.splitext(os.path.join(root,file))[1]==self.filetypeinput.get(1.0,END).strip():
                        self.txtt.insert(INSERT, os.path.join(root,file) + "\n")
                        self.filea.append(os.path.join(root,file))
            print(self.filea)
    def find_files(self):
        if not self.a.get() or self.a.get() == "开始文件读取，筛选操作前请先选择文件————————>":
            messagebox.showinfo(" ", "请先选择文件")
        elif self.filenameinput.get(1.0, END) == "\n":
            messagebox.showinfo(" ", "请输入筛选的文件名称")
        else:
            self.filea = []
            self.txtt.delete(1.0, END)
            files = os.walk(self.c)
            for root, dir, filelist in files:
                for file in filelist:
                    if  self.filenameinput.get(1.0,END).strip()==file:
                        self.txtt.insert(INSERT, os.path.join(root, file) + "\n")
                        self.filea.append(os.path.join(root, file))
            print(self.filea)


    def bofilexuanzhe(self):
        self.c= filedialog.askdirectory()
        self.a.set(self.c)
    def bofilemove(self):
        self.c= filedialog.askdirectory()
        self.movevar.set(self.c)
    def bofilesize(self):
        self.shaixuan(self.filesizeinputstra.get(1.0,END),self.filesizeinputendd.get(1.0,END))
    def clickfile(self):
        if self.a.get()=="开始文件读取，筛选操作前请先选择文件————————>"or not self.a.get():
            messagebox.showinfo(" ", "请先选择文件")
        else:
            self.filea = []
            self.txtt.delete(1.0, END)
            files = os.walk(self.c)
            self.num = 0
            for root, dir, filelist in files:
                for file in filelist:
                    self.txtt.insert(INSERT, os.path.join(root, file) + "\n")
                    # self.filevalues=open(os.path.join(root, file),"r",encoding='UTF-8')
                    self.filesize = os.path.getsize(os.path.join(root, file))
                    self.filea.append(os.path.join(root, file))
                    self.num += self.filesize
                    self.sizeif(self.filesize)
            print(self.filea)

            self.sizeif(self.num)
            # print(self.filevalues.readlines())
            # self.filevalues.close()

    def shaixuan(self,sta,en):
        if self.a.get()=="开始文件读取，筛选操作前请先选择文件————————>" or not self.a.get():
            messagebox.showinfo(" ", "请先选择文件")
        elif sta=="\n" or  en=="\n":
            messagebox.showinfo(" ","请输入筛选的开始值与结束值")
        else:
            self.filea = []
            self.txtt.delete(1.0, END)
            files = os.walk(self.c)
            for root, dir, filelist in files:
                for file in filelist:
                    self.filesize = os.path.getsize(os.path.join(root, file))
                    if int(sta) < self.filesize < int(en):
                        self.filea.append(os.path.join(root, file))
                        self.txtt.insert(INSERT, os.path.join(root, file) + "\n")
                        self.sizeif(self.filesize)
            print(self.filea)

    ##计算文件大小的方法
    def sizeif(self,m):
        if m < 1024:
            self.txtt.insert(INSERT, "size:" + str(m) + "B" + "\n")
        elif m < 1024000:
            m =m / 1024
            self.txtt.insert(INSERT, "size:" + str(m) + "K" + "\n")
        elif m < 1024000000:
            m = m / 1024000
            self.txtt.insert(INSERT, "size:" + str(m) + "M" + "\n")
        else:
            m = m / 1024000000
            self.txtt.insert(INSERT, "size:" + str(m) + "G" + "\n")


    #def ezj(self):
    #    self.me = messagebox.showinfo("wait","让我看看你有什么好东西")
     #   files=os.walk("E:/")
    #    for root,dir,filelist in files:
     #       for file in filelist:
     #           self.txtt.insert(INSERT, os.path.join(root, file)+"\n")
                #self.filevalues=open(os.path.join(root, file),"r",encoding='UTF-8')
                #print(self.filevalues.readlines())
                #self.filevalues.close()
      #  self.me=messagebox.showinfo("扫黄","就知道你小子不老实")








app().mainloop()